package com.czwj.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.czwj.entities.BudgetItem;
import com.czwj.entities.DataGridData;
import com.czwj.entities.Equipment;
import com.czwj.entities.EquipmentMark;
import com.czwj.entities.Notice;
import com.czwj.entities.PageBean;
import com.czwj.entities.PlanMain;
import com.czwj.entities.ProjectItem;
import com.czwj.entities.SafetyInspection;
import com.czwj.entities.UserInfo;
import com.czwj.entities.WarningValue;
import com.czwj.service.BudgetItemService;
import com.czwj.service.EquipmentMarkService;
import com.czwj.service.EquipmentService;
import com.czwj.service.NoticeService;
import com.czwj.service.PlanMainService;
import com.czwj.service.ProjectItemService;
import com.czwj.service.SafetyInspectionService;
import com.czwj.service.WarningValueService;
import com.czwj.util.TextUtil;

@Controller
public class WarningTipsAction{
	@Resource
	ProjectItemService projectItemService;
	@Resource
	BudgetItemService budgetItemService;
	@Resource
	WarningValueService warningValueService;
	@Resource
	PlanMainService planMainService;
	@Resource
	NoticeService noticeService;
	@Resource
	SafetyInspectionService safetyInspectionService;
	@Resource
	EquipmentService equipmentService;
	@Resource
	EquipmentMarkService equipmentMarkService;
	
	@RequestMapping(value = "/warningMoney.do",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<Map<String,Object>> warningMoney(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows) throws Exception{
		DataGridData<Map<String,Object>> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo user = ((UserInfo)currentUser.getSession().getAttribute("userInfo"));
		Map<String,Object> map2 = new HashMap<String,Object>();
		Map<String,Object> map3 = new HashMap<String,Object>();
		List<ProjectItem> list = new ArrayList<>();
		List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
		if(user.getRoleid() == 1 || user.getRoleid() == 2){
			map.put("userid",user.getId());
			map.put("status",2);
			list = projectItemService.searchforlevel1(map);
		}else if(user.getRoleid() == 3){
			map.put("userid",user.getId());
			map.put("status",2);
			list = projectItemService.searchforlevel2(map);
		}else{
			map.put("userid",user.getId());
			map.put("status",2);
			list = projectItemService.searchforlevel3(map);
		}
		for(int i = 0;i < list.size();i++){
			map2.put("projectitemid",list.get(i).getId());
			List<BudgetItem> itemlist = budgetItemService.search(null);
			for(BudgetItem budgetItem:itemlist){
				map2.put("budgetitemid",budgetItem.getId());
				map3 = budgetItemService.warningtips(map2);
				if(map3 != null){
					if(Double.valueOf(map3.get("budgetmoney").toString()) != 0){
						listMap.add(map3);
					}
				}
			}
		}
		Map<String,Object> amap = new HashMap<String,Object>();
		amap.put("companyid",user.getCompanyid());
		amap.put("content","资金");
		WarningValue warningValue = warningValueService.selectWarning(amap);
		List<Map<String,Object>> listMap2 = new ArrayList<Map<String,Object>>();
		for(int k = 0;k < listMap.size();k++){
			Double percent = Double.valueOf(listMap.get(k).get("executemoney").toString())
					/ (Double.valueOf(listMap.get(k).get("budgetmoney").toString())
							+ Double.valueOf(listMap.get(k).get("applymoney").toString()));
			if(percent * 100 >= warningValue.getPercent()){
				listMap.get(k).put("remainmoney",
						(Double.valueOf(listMap.get(k).get("budgetmoney").toString())
								+ Double.valueOf(listMap.get(k).get("applymoney").toString())
								- Double.valueOf(listMap.get(k).get("executemoney").toString())));
				listMap2.add(listMap.get(k));
				Notice notice = new Notice();
				System.out.println(listMap.get(k).get("id"));
				ProjectItem projectItem = projectItemService.selectByPrimaryKey((Integer)listMap.get(k).get("id"));
				notice.setTargetid(projectItem.getCreaterid());
				notice.setType(1);
				notice.setStatus(0);
				notice.setContent(projectItem.getName() + "的" + listMap.get(k).get("content").toString() + "超额");
				notice.setCreatedate(new Date());
				List<Notice> list2 = noticeService.searchbyuser(projectItem.getCreaterid());
				Boolean flag = false;
				for(Notice notice2:list2){
					if(notice2.getContent().equals(notice.getContent())){
						flag = true;
					}
				}
				if(!flag){
					noticeService.insertSelective(notice);
				}
			}
		}
		result.setRows(listMap2);
		result.setTotal(listMap2.size());
		return result;
	}
	
	@RequestMapping(value = "/warningTime.do",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<PlanMain> warningTime(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows) throws Exception{
		DataGridData<PlanMain> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo user = ((UserInfo)currentUser.getSession().getAttribute("userInfo"));
		Map<String,Object> map2 = new HashMap<String,Object>();
		List<PlanMain> planlist = new ArrayList<PlanMain>();
		List<PlanMain> planmainlist = new ArrayList<PlanMain>();
		if(user.getRoleid() == 1){
			map.put("userid",user.getId());
			map.put("status",2);
			List<ProjectItem> list = projectItemService.searchforlevel0(map);
			for(int i = 0;i < list.size();i++){
				map2.put("projectitemid",list.get(i).getId());
				map2.put("status",4);
				planlist = planMainService.searchbyprojectitem(map2);
				for(PlanMain planMain:planlist){
					if(planMain != null){
						planmainlist.add(planMain);
					}
				}
			}
		}else if(user.getRoleid() == 2){
			map.put("userid",user.getId());
			map.put("status",2);
			List<ProjectItem> list = projectItemService.searchforlevel1(map);
			for(int i = 0;i < list.size();i++){
				map2.put("projectitemid",list.get(i).getId());
				map2.put("status",4);
				planlist = planMainService.searchbyprojectitem(map2);
				for(PlanMain planMain:planlist){
					if(planMain != null){
						planmainlist.add(planMain);
					}
				}
			}
		}else if(user.getRoleid() == 3){
			map.put("userid",user.getId());
			map.put("status",2);
			List<ProjectItem> list = projectItemService.searchforlevel2(map);
			for(int i = 0;i < list.size();i++){
				map2.put("projectitemid",list.get(i).getId());
				map2.put("status",4);
				planlist = planMainService.searchbyprojectitem(map2);
				for(PlanMain planMain:planlist){
					if(planMain != null){
						planmainlist.add(planMain);
					}
				}
			}
		}else{
			map.put("userid",user.getId());
			map.put("status",2);
			List<ProjectItem> list = projectItemService.searchforlevel3(map);
			for(int i = 0;i < list.size();i++){
				map2.put("projectitemid",list.get(i).getId());
				map2.put("status",4);
				planlist = planMainService.searchbyprojectitem(map2);
				for(PlanMain planMain:planlist){
					if(planMain != null){
						planmainlist.add(planMain);
					}
				}
			}
		}
		Map<String,Object> amap = new HashMap<String,Object>();
		amap.put("companyid",user.getCompanyid());
		amap.put("content","进度");
		WarningValue warningValue = warningValueService.selectWarning(amap);
		List<PlanMain> planmainlist2 = new ArrayList<PlanMain>();
		for(int k = 0;k < planmainlist.size();k++){
			Double percent = (double)(System.currentTimeMillis() - planmainlist.get(k).getStartdate().getTime())
					/ (planmainlist.get(k).getEnddate().getTime() - planmainlist.get(k).getStartdate().getTime());
			if(percent * 100 >= warningValue.getPercent()){
				planmainlist2.add(planmainlist.get(k));
				Notice notice = new Notice();
				ProjectItem projectItem = projectItemService.selectByPrimaryKey(planmainlist.get(k).getProjectitemid());
				notice.setTargetid(projectItem.getCreaterid());
				notice.setType(1);
				notice.setStatus(0);
				notice.setContent(projectItem.getName() + "的" + planmainlist.get(k).getName() + "超期");
				notice.setCreatedate(new Date());
				List<Notice> list = noticeService.searchbyuser(projectItem.getCreaterid());
				Boolean flag = false;
				for(Notice notice2:list){
					if(notice2.getContent().equals(notice.getContent())){
						flag = true;
					}
				}
				if(!flag){
					noticeService.insertSelective(notice);
				}
			}
		}
		result.setRows(planmainlist2);
		result.setTotal(planmainlist2.size());
		return result;
	}
	
	@RequestMapping(value = "/appWarningMoney.do",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<Map<String,Object>> appWarningMoney(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "projectitemid",required = false) String projectitemid) throws Exception{
		DataGridData<Map<String,Object>> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo user = ((UserInfo)currentUser.getSession().getAttribute("userInfo"));
		Map<String,Object> map2 = new HashMap<String,Object>();
		Map<String,Object> map3 = new HashMap<String,Object>();
		List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
		if(user.getRoleid() == 1 || user.getRoleid() == 2){
			if(TextUtil.isNotEmpty(projectitemid)){
				map2.put("projectitemid",projectitemid);
			}
			List<BudgetItem> itemlist = budgetItemService.search(null);
			for(BudgetItem budgetItem:itemlist){
				map2.put("budgetitemid",budgetItem.getId());
				map3 = budgetItemService.warningtips(map2);
				if(map3 != null){
					if(Double.valueOf(map3.get("budgetmoney").toString()) != 0){
						listMap.add(map3);
					}
				}
			}
		}else if(user.getRoleid() == 3){
			if(TextUtil.isNotEmpty(projectitemid)){
				map2.put("projectitemid",projectitemid);
			}
			List<BudgetItem> itemlist = budgetItemService.search(null);
			for(BudgetItem budgetItem:itemlist){
				map2.put("budgetitemid",budgetItem.getId());
				map3 = budgetItemService.warningtips(map2);
				if(map3 != null){
					if(Double.valueOf(map3.get("budgetmoney").toString()) != 0){
						listMap.add(map3);
					}
				}
			}
		}else{
			if(TextUtil.isNotEmpty(projectitemid)){
				map2.put("projectitemid",projectitemid);
			}
			List<BudgetItem> itemlist = budgetItemService.search(null);
			for(BudgetItem budgetItem:itemlist){
				map2.put("budgetitemid",budgetItem.getId());
				map3 = budgetItemService.warningtips(map2);
				if(map3 != null){
					if(Double.valueOf(map3.get("budgetmoney").toString()) != 0){
						listMap.add(map3);
					}
				}
			}
		}
		WarningValue warningValue = warningValueService.selectByPrimaryKey(2);
		List<Map<String,Object>> listMap2 = new ArrayList<Map<String,Object>>();
		for(int k = 0;k < listMap.size();k++){
			Double percent = Double.valueOf(listMap.get(k).get("executemoney").toString())
					/ (Double.valueOf(listMap.get(k).get("budgetmoney").toString())
							+ Double.valueOf(listMap.get(k).get("applymoney").toString()));
			if(percent * 100 >= warningValue.getPercent()){
				listMap.get(k).put("remainmoney",
						(Double.valueOf(listMap.get(k).get("budgetmoney").toString())
								+ Double.valueOf(listMap.get(k).get("applymoney").toString())
								- Double.valueOf(listMap.get(k).get("executemoney").toString())));
				listMap2.add(listMap.get(k));
			}
		}
		result.setRows(listMap2);
		result.setTotal(listMap2.size());
		return result;
	}
	
	@RequestMapping(value = "/appWarningTime.do",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<PlanMain> appWarningTime(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "projectitemid",required = false) String projectitemid) throws Exception{
		DataGridData<PlanMain> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo user = ((UserInfo)currentUser.getSession().getAttribute("userInfo"));
		Map<String,Object> map2 = new HashMap<String,Object>();
		List<PlanMain> planlist = new ArrayList<PlanMain>();
		List<PlanMain> planmainlist = new ArrayList<PlanMain>();
		if(user.getRoleid() == 1 || user.getRoleid() == 2){
			map2.put("userid",user.getId());
			if(TextUtil.isNotEmpty(projectitemid)){
				map2.put("projectitemid",projectitemid);
			}
			map2.put("status",4);
			planlist = planMainService.searchbyprojectitem(map2);
			for(PlanMain planMain:planlist){
				if(planMain != null){
					planmainlist.add(planMain);
				}
			}
		}else if(user.getRoleid() == 3){
			map2.put("userid",user.getId());
			if(TextUtil.isNotEmpty(projectitemid)){
				map2.put("projectitemid",projectitemid);
			}
			map2.put("status",4);
			planlist = planMainService.searchbyprojectitem(map2);
			for(PlanMain planMain:planlist){
				if(planMain != null){
					planmainlist.add(planMain);
				}
			}
		}else{
			map2.put("userid",user.getId());
			if(TextUtil.isNotEmpty(projectitemid)){
				map2.put("projectitemid",projectitemid);
			}
			map2.put("status",4);
			planlist = planMainService.searchbyprojectitem(map2);
			for(PlanMain planMain:planlist){
				if(planMain != null){
					planmainlist.add(planMain);
				}
			}
		}
		WarningValue warningValue = warningValueService.selectByPrimaryKey(1);
		List<PlanMain> planmainlist2 = new ArrayList<PlanMain>();
		for(int k = 0;k < planmainlist.size();k++){
			Double percent = (double)(System.currentTimeMillis() - planmainlist.get(k).getStartdate().getTime())
					/ (planmainlist.get(k).getEnddate().getTime() - planmainlist.get(k).getStartdate().getTime());
			if(percent * 100 >= warningValue.getPercent()){
				planmainlist2.add(planmainlist.get(k));
			}
		}
		result.setRows(planmainlist2);
		result.setTotal(planmainlist2.size());
		return result;
	}

	@RequestMapping(value = "/warningSafe.do",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<SafetyInspection> warningSafe(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "projectitemid",required = false) String projectitemid) throws Exception{
		DataGridData<SafetyInspection> result = new DataGridData();
		Map<String, Object> map = new HashMap<String, Object>();
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		map.put("companyid",userInfo.getCompanyid());
		List<SafetyInspection> list = safetyInspectionService.selectTosafe(map);
		result.setRows(list);
		result.setTotal(list.size());
		return result;
	}
	
	@RequestMapping(value = "/warningEquipment.do",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<EquipmentMark> warningEquipmentMark(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows) {
		DataGridData<EquipmentMark> result = new DataGridData();
		List<EquipmentMark> equipmentMarks = equipmentMarkService.searchWarning();
		List<EquipmentMark> equipmentMarks2 = equipmentMarkService.searchWarning2();
		equipmentMarks.addAll(equipmentMarks2);
		result.setRows(equipmentMarks);
		result.setTotal(equipmentMarks.size());
		return result;
	}
}
